package com.miaosha.redis.hash;

import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * 博客案例
 *
 * @author liushiqiao
 * @create 2021-10-09 7:54
 **/
public class Blog {

    private Jedis jedis = new Jedis("182.42.113.98", 6379);

    /**
     * 发布博客
     *
     * @param id
     * @param title
     * @param content
     * @param author
     * @param time
     */
    public void publishBlog(long id, String title, String content, String author, String time) {

        Long publishBlogResult = jedis.msetnx("article:" + id + ":title", title,
                "article:" + id + ":content", content,
                "article:" + id + ":author", author,
                "article:" + id + ":time", time);

        long length = jedis.strlen("article:" + id + ":content");
        jedis.setnx("article:" + id + ":content_length", String.valueOf(length));

        String modifyResult = jedis.mset("article:1:title", "修改后-学习redis",
                "article:1:content", "修改后-如何学号redis的使用",
                "article:1:author", "修改后-中华石杉",
                "article:1:time", "2021-01-01 00:00:00");
        List<String> modifyBlog = jedis.mget("article:1:title", "article:1:content",
                "article:1:author",
                "article:1:time");
        System.out.println(jedis.get("article:1:title"));
    }

    /**
     * 获取博客
     *
     * @param id
     * @return
     */
    public List<String> getBlog(long id) {
        return jedis.mget("article:" + id + ":title", "article:" + id + ":content",
                "article:" + id + ":author",
                "article:" + id + ":time");
    }

    /**
     * 更新博客
     *
     * @param id
     * @param title
     * @param content
     */
    public void updateBlog(long id, String title, String content) {
        jedis.mset("article:" + id + ":title", title,
                "article:" + id + ":content", content);
    }

    /**
     * 预览
     *
     * @param id
     * @return
     */
    public String previewBlog(long id) {
        return jedis.getrange("article:" + id + ":content", 0, 10);
    }


    /**
     * 对博客进行点赞
     *
     * @param id
     */
    public void likeBlog(long id) {
        jedis.incr("article:" + id + ":like_count");
    }
}
